/**
 * Class Text Input Module.
 * 
 * @author Maxime Ollagnier
 */
InputTextModule.inherits(InputModule);
function InputTextModule(params) {
	params = Util.getObject(params);
	this.parentConstructor(InputModule, params);
	this.setOnKeyUp(params.onKeyUp);
	this.setPassworded(params.passworded);
	this.setSpellcheck(params.spellcheck);
}

/** Default values */
InputTextModule.prototype.DEFAULT_ON_KEY_UP = function(){};
InputTextModule.prototype.DEFAULT_PASSWORDED = false;
InputTextModule.prototype.DEFAULT_SPELLCHECK = false;

/**
 * Fills the given jQuery element
 */
InputTextModule.prototype.fillJQ = function(jQ) {
	jQ.append($('<label></label>').append(this.label).append(this.mandatory ? '*' : ''));
	jQ.append($('<div class="input">' +
			'<input id="' + (Util.checkString(this.id) ? this.id : '') + '" type="'+ (this.passworded ? 'password' : 'text') +'" placeholder="' + this.placeholder + '" spellcheck="' + this.spellcheck + '" class="' + this.widthClass + '" value="' + this.value.toString().replace(/"/g, '&quot;') + '"' + (this.readonly ? ' readonly="true"' : '') + '>' +
			(Util.checkStringNotEmpty(this.description) ? '<div class="description">' + this.description + '</div>' : '') +
	'</div>'));
	var that = this;
	jQ.find('input:first').keyup(function(event) {
		that.setValue($(this).val());
		that.modified = true;
		Util.getFunction(that.onKeyUp)(that.value, event.which);
		if (that.validate() && event.which == 13) {
			Util.getFunction(that.onEnter)(that.value);
		}
	})
	.blur(function(event){
		that.onBlur();
	});
	
	return jQ;
};

/**
 * Sets the onKeyUp callback function
 * 
 * @param onKeyUp Function
 */
InputTextModule.prototype.setOnKeyUp = function(onKeyUp) {
	this.onKeyUp = onKeyUp;
	if (!Util.checkFunction(onKeyUp)) {
		this.onKeyUp = this.DEFAULT_ON_KEY_UP;
	}
	return this;
};

/**
 * Sets the passworded field
 * @param pwded boolean, true if this input text is a password, false otherwise
 */
InputTextModule.prototype.setPassworded = function(pwded) {
	this.passworded = pwded;
	if (!Util.checkBool(pwded)) {
		this.passworded = this.DEFAULT_PASSWORDED;
	}
	return this;
};

/**
 * Sets the spellcheck field
 * @param spellcheck boolean, true if this input text is spellcheck, false otherwise
 */
InputTextModule.prototype.setSpellcheck = function(spellcheck) {
	this.spellcheck = spellcheck;
	if (!Util.checkBool(spellcheck)) {
		this.spellcheck = this.DEFAULT_SPELLCHECK;
	}
	return this;
};

/**
 * Add a validation method to the given inputs to check that the concatenation of the two inputs has not a length greater than len (default len is 38)
 * 
 * @param input1 the first input to be checked
 * @param input2 the second input to be checked with
 * @param len the max length of the concatenation of the two inputs. Default len is 38. The check is made with an additional space between the values of the two inputs.
 */
InputTextModule.linkInputsForLength = function(input1, input2, len){
	if (!Util.checkNumber(len)){ len = 38; }
	input1.__oldValidationMethod = input1.validationMethod;
	input1.validationMethod = new ValidationMethod({
		'errorMessage' : Util.checkObject(input1.__oldValidationMethod) ? input1.__oldValidationMethod.errorMessage : I18n.get('invalid.input.linkedlength', false, [input1.label, input2.label, len]),
		'method' : function(v){
			if (input1.value.length + input2.value.length + 1 > len){
				return false;
			}
			if (Util.checkObject(input1.__oldValidationMethod)){
				return input1.__oldValidationMethod.method(v);
			}
			return true;
		}
	});
	input1.__oldOnKeyUp = input1.onKeyUp;
	input1.onKeyUp = function(v){
		input1.__oldOnKeyUp(v);
		input2.validate();
	};
	
	input2.__oldValidationMethod = input2.validationMethod;
	input2.validationMethod = new ValidationMethod({
		'errorMessage' : Util.checkObject(input2.__oldValidationMethod) ? input2.__oldValidationMethod.errorMessage : I18n.get('invalid.input.linkedlength', false, [input1.label, input2.label, len]),
		'method' : function(v){
			if (input1.value.length + input2.value.length + 1 > len){
				return false;
			}
			if (Util.checkObject(input2.__oldValidationMethod)){
				return input2.__oldValidationMethod.method(v);
			}
			return true;
		}
	});
	input2.__oldOnKeyUp = input2.onKeyUp;
	input2.onKeyUp = function(v){
		input2.__oldOnKeyUp(v);
		input1.validate();
	};
};
